package com.leyou.upload.controller;

import com.leyou.upload.service.UpLoadService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

/**
 * @Description:
 * @author: Mr_Sun
 * @Date: 2018/9/3
 * @Time: 18:22
 */
@RestController
@RequestMapping("upload")
public class UpLoadController {
    @Autowired
    private UpLoadService upLoadService;
    @PostMapping("image")
    public ResponseEntity<String> uploadImage(@RequestParam("file") MultipartFile file) {

       String url= upLoadService.upload(file);
//        System.out.println(url);
        if (StringUtils.isBlank(url)) {
            //url为空  上传失败

            return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(null);
        }

        // 返回200，并且携带url路径
        return ResponseEntity.ok(url);
    }
}
